import java.awt.geom.Point2D.Double;

public class Car {
   private int x=0;					//Position X-Coordinate
   private int y=0;					//Position Y-Coordinate
   private double vX=0;				//Speed X
   private double vY=0;				//Speed Y
   private int checkpoint=0;		//Number of the next checkpoint it has to pass
   private int maximumspeed = 3;	//Maximum speed
   private int direction = 0;		//Direction
   private double speed = 0;		//Current speed
   private int steeringspeed = 30;	//How strong it steers
   private double carReward =0;		//Used for the reward it gets for exploring unknown territory
   public char name;				//A 'name' to distinguish it from the other cars.

   
   //Returns its current direction
   public int getDirection(){
	   return this.direction;
   }
   
   //Alters the direction it's heading   
   public void steer(int wheelturn) {
	   this.direction += wheelturn;
	   if(this.direction>=360) this.direction -= 360;
	   if(this.direction<0) this.direction += 360;
   }
   
   //Returns its current speed
   public double getSpeed() {
	   return speed;
   }
   
   //Returns its maximum speed
   public double getMaxSpeed() {
	   return maximumspeed;
   }
   
   //Accelerates and decelerates. Can't go over its maxspeed, can't go under 0.
   public void celerate(int vSpeed) {
	   this.speed+= vSpeed;
	   if(this.speed<0) this.speed=0;
	   if(this.speed>maximumspeed) this.speed= maximumspeed;	 
	   //System.out.println("Speed " + speed);
   }
   
   //Updates the speed and the steering according to an action.
   public void update(Double action) {
	   celerate((int) action.x);
	   steer((int) action.y * steeringspeed);
	   updateVxVy();
   }
   
   //Calculates the speed in X and Y direction from the direction and speed.
   public void updateVxVy() {
		this.vX = Math.cos(Math.toRadians(direction)) * speed;
		this.vY = Math.sin(Math.toRadians(direction)) * speed;
	}
   
   //Constructor. At the start the car is given the x/y position and the maxspeed allowed.
   public Car(int newX, int newY, int maxspeed){
       x=newX;
       y=newY;
       maximumspeed = maxspeed;
   }

   //Returns the car reward.
   public double getCarR(){
       return this.carReward;
   }
   
   //Returns the X coordinate
   public int getX(){
       return this.x;
   }

   //Returns the Y coordinate
   public int getY(){
       return this.y;
   }

   //Returns the X speed
   public double getVX(){
       return this.vX;
   }

   //Returns the Y speed
   public double getVY(){
       return this.vY;
   }
   
   //Increases the speed by a certain dvx&dvy. Was used in a previous version.
   public void updateSpeeds(double dvx, double dvy) {
	   vX += dvx;
	   vY += dvy;
	   //if(vX>maximumspeed) vX = maximumspeed;
	   //if(vX<-maximumspeed) vX = -maximumspeed;
	   //if(vY>maximumspeed) vY = maximumspeed;
	   //if(vY<-maximumspeed) vY = -maximumspeed;
   }
   
   //Increases the car reward.
   public void setReward(double reward){
	   carReward = carReward + reward;
   }
   
   //Resets the reward.
   public void resetReward(){
	   carReward = 0;
   }

   //Changes the X coordinate
   public void setX(int x) {
	   this.x = x;
   }
   
   //Changes the Y coordinate
   public void setY(int y) {
	   this.y = y;
   }
   
   //Changes the X speed (not used anymore)
   public void setVX(double vX) {
	   this.vX = vX;
   }
   
   //Changes the Y speed (not used anymore)
   public void setVY(double vY) {
	   this.vY = vY;
   }
   
   //Updates what checkpoint it has to pass
   public void setCheckpoint(int check) {
	   this.checkpoint = check;
   }
   
   //Increases the checkpoint by 1
   public void incCheckpoint() {
	   this.checkpoint++;
   }
   
   //Returns which checkpoint it has to pass
   public int getCheckpoint() {
	   return checkpoint;
   }
   
  
   
}
